package com.ruoyi.web.utils.wuqing;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.domain.function.FunctionConfig;
import com.ruoyi.web.domain.insect.InsectRecord;
import com.ruoyi.web.domain.warning.FactorInfo;
import com.ruoyi.web.pojo.dto.devicedata.DeviceFactorCountData;
import com.ruoyi.web.pojo.request.devicedata.DeviceFactorDataRequest;
import com.ruoyi.web.pojo.request.devicedata.DevicePhotoRequest;
import com.ruoyi.web.pojo.request.devicedata.DeviceRainCountRequest;
import com.ruoyi.web.pojo.request.insect.*;
import com.ruoyi.web.pojo.vo.devicedata.DeviceFactorDataVO;
import com.ruoyi.web.pojo.vo.devicedata.DevicePhotoVO;
import com.ruoyi.web.pojo.vo.devicedata.PropertyDataVO;
import com.ruoyi.web.pojo.vo.insect.InsectAnalysisVO;
import com.ruoyi.web.pojo.vo.insect.InsectCountDataVO;
import com.ruoyi.web.pojo.vo.insect.InsectHistoryDataVO;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class WQRremoteCall {

    //private static final String BASE_URL = "http://yun.bigdata5s.com/api";
    private static final String BASE_URL = "http://127.0.0.1:30055";


    /**
     * @desc  设备图片 - 查询设备图片
     * @author fwx 2024/6/12 17:30
     **/
    public static TableDataInfo<DevicePhotoVO> queryPhotoPage(DevicePhotoRequest request){
        UrlBuilder builder = request.getQuery();
        String body = HttpRequest.get(String.format("%s/device-photo/getPhotosPage?%s",BASE_URL, builder.getQuery().toString()))
                .execute()
                .body();
        TableDataInfo<JSONObject> jsonDatas = JSONObject.parseObject(body,TableDataInfo.class);

        List<DevicePhotoVO> vos = new ArrayList<>();
        for (JSONObject json : jsonDatas.getRows()) {
            vos.add(json.to(DevicePhotoVO.class));
        }

        TableDataInfo<DevicePhotoVO> v2Devices = new TableDataInfo<>();
        v2Devices.setRows(vos);
        v2Devices.setTotal(jsonDatas.getTotal());
        v2Devices.setMsg(jsonDatas.getMsg());
        v2Devices.setCode(jsonDatas.getCode());

        return v2Devices;
    }

    /**
     * @desc  查询所有因子 - 传感器因子
     * @author fwx 2024/6/13 15:16
     **/
    public static List<FactorInfo> getFactorAll(){
        String url = String.format("%s/factor-warning/getFactorAll",BASE_URL);
        return getRremoteList(FactorInfo.class,url);
    }
    
    /**
     * @desc  分类查询因子 - 传感器因子
     * @author fwx 2024/6/15 10:03
     **/
    public static List<FactorInfo> getFactorByType(String type){
        UrlBuilder builder = UrlBuilder.of();
        builder.addQuery("type",type);
        String url = String.format("%s/factor-warning/getFactorByType?%s",BASE_URL, builder.getQuery().toString());
        return getRremoteList(FactorInfo.class,url);
    }



    /**
     * @desc  根据设备ID(多个),查询按月,年,日的统计数据 - 虫情识别记录
     * @author fwx 2024/6/13 16:37
     **/
    public static List<InsectCountDataVO> getUserInsectCountData(InsectCountDataRequest request){
        UrlBuilder builder = request.getQuery();

        String body = HttpRequest.get(String.format("%s/insect-record/getUserInsectCountData?%s",BASE_URL, builder.getQuery().toString()))
                .execute()
                .body();

        String result = JSONObject.parseObject(body).getString("data");

        List<InsectCountDataVO> dataVOS = JSONArray.parseArray(result, InsectCountDataVO.class);

        return dataVOS;
    }

    /**
     * @desc  获取昆虫始见期的时间 - 虫情识别记录
     * @author fwx 2024/6/13 16:37
     **/
    public static List<InsectCountDataVO> getInsectStartTime(String insects,InsectCountDataRequest request){
        UrlBuilder builder = request.getQuery();
        builder.addQuery("insects",insects);

        String body = HttpRequest.get(String.format("%s/insect-record/getInsectStartTime?%s",BASE_URL, builder.getQuery().toString()))
                .execute()
                .body();

        String result = JSONObject.parseObject(body).getString("data");

        List<InsectCountDataVO>  dataVOS = JSONArray.parseArray(result, InsectCountDataVO.class);

        return dataVOS;
    }
    
    /**
     * @desc 虫情数据页-统计设备识别虫数量(左上) - 虫情识别记录
     * @author fwx 2024/6/13 17:38
     **/
    public static List<InsectCountDataVO> getSpInsectCountData(InsectSumDataRequest request){
        UrlBuilder builder = request.getQuery();

        String body = HttpRequest.get(String.format("%s/insect-record/getSpInsectCountData?%s",BASE_URL, builder.getQuery().toString()))
                .execute()
                .body();

        String result = JSONObject.parseObject(body).getString("data");

        List<InsectCountDataVO>  dataVOS = JSONArray.parseArray(result, InsectCountDataVO.class);

        return dataVOS;
    }

    /**
     * @desc  虫情数据页(右上)-虫情历史数据 - 虫情识别记录
     * @author fwx 2024/6/14 11:21
     **/
    public static InsectHistoryDataVO getHistoryHandlerData(InsectHistoryDataRequest request){
        UrlBuilder builder = request.getQuery();
        String url = String.format("%s/insect-record/getHistoryHandlerData?%s",BASE_URL, builder.getQuery().toString());
        return getRremoteObject(InsectHistoryDataVO.class,url);
    }

    /**
     * @desc  虫情数据页(底部) - 虫情统计数据- 虫情识别记录
     * @author fwx 2024/6/14 11:21
     **/
    public static List<InsectRecord> getHistoryData(InsectHistoryDataRequest request){
        UrlBuilder builder = request.getQuery();
        String url = String.format("%s/insect-record/getHistoryData?%s",BASE_URL, builder.getQuery().toString());
        return getRremoteList(InsectRecord.class,url);
    }
    
    /**
     * @desc  虫情数据页(底部 表格头) - 识别种类,识别数量 - 虫情识别记录
     * @author fwx 2024/6/14 11:38
     **/
    public static InsectCountDataVO insectStatistics(InsectSumDataRequest request){
        UrlBuilder builder = request.getQuery();
        String url = String.format("%s/insect-record/insectStatistics?%s",BASE_URL, builder.getQuery().toString());
        return getRremoteObject(InsectCountDataVO.class,url);
    }
    
    /**
     * @desc 获取识别的原始记录数据 - 虫情识别记录
     * @author fwx 2024/6/14 15:10
     **/
    public static List<InsectRecord> getOriginHistoryData(InsectTopRequest request){
        UrlBuilder builder = request.getQuery();
        String url = String.format("%s/insect-record/getOriginHistoryData?%s",BASE_URL, builder.getQuery().toString());
        return getRremoteList( InsectRecord.class,url);
    }
    
    /**
     * @desc  查询指定设备下识别出的昆虫名 - 虫情识别记录
     * @author fwx 2024/6/14 15:29
     **/
    public static Set<String> getInsctName(InsectTopRequest request){
        UrlBuilder builder = request.getQuery();
        String url = String.format("%s/insect-record/getInsctName?%s",BASE_URL, builder.getQuery().toString());
        List<String> records = getRremoteList(String.class,url);
        Set<String> insect = records.stream().collect(Collectors.toSet());
        return insect;
    }
    
    /**
     * @desc  根据昆虫名和日期按日统计 - 虫情识别记录
     * @author fwx 2024/6/14 16:27
     **/
    public static List<InsectAnalysisVO> getCountByInsectName(InsectListRequest request){
        UrlBuilder builder = request.getQuery();
        String url = String.format("%s/insect-record/getCountByInsectName?%s",BASE_URL, builder.getQuery().toString());
        return getRremoteList( InsectAnalysisVO.class,url);
    }
    
    /**
     * @desc  数据分析 - 查询某一类虫的图片 - 虫情图片
     * @author fwx 2024/6/14 16:41
     **/
    public static List<DevicePhotoVO> getByInsectName(String deviceId, String time, String insectName){
        UrlBuilder builder = UrlBuilder.of();
        builder.addQuery("deviceId",deviceId);
        builder.addQuery("time",time);
        builder.addQuery("insectName",insectName);
        String url = String.format("%s/device-photo/getByInsectName?%s",BASE_URL, builder.getQuery().toString());
        return getRremoteList(DevicePhotoVO.class,url);
    }
    
    /**
     * @desc  根据code查询使用中的配置 - 功能配置
     * @author fwx 2024/6/14 16:51
     **/
    public static FunctionConfig getUseConfigByCode(String code){
        UrlBuilder builder = UrlBuilder.of();
        builder.addQuery("code",code);
        String url = String.format("%s/function-config/getCountByInsectName?%s",BASE_URL, builder.getQuery().toString());
        return getRremoteObject(FunctionConfig.class,url);
    }

    /**
     * @desc  多设备单因子 - 基础数据 - 传感器数据
     * @author fwx 2024/6/14 17:05
     **/
    public static List<DeviceFactorDataVO> getBaseData(DeviceFactorDataRequest request){

        String url = String.format("%s/insect-situation/getBaseData",BASE_URL);
        String body = HttpRequest.post(url)
                .body(JSONObject.toJSONString(request))
                .execute()
                .body();

        String result = JSONObject.parseObject(body).getString("data");

        List<DeviceFactorDataVO> vos = JSONArray.parseArray(result, DeviceFactorDataVO.class);

        return vos;
    }

    /**
     * @desc  数据分析 - 多台多因子 - 传感器数据
     * @author fwx 2024/6/15 9:50
     **/
    public static List<DeviceFactorDataVO> getFactorData(DeviceFactorDataRequest request){

        String url = String.format("%s/insect-situation/getFactorData",BASE_URL);
        String body = HttpRequest.post(url)
                .body(JSONObject.toJSONString(request))
                .execute()
                .body();

        String result = JSONObject.parseObject(body).getString("data");

        List<DeviceFactorDataVO> vos = JSONArray.parseArray(result, DeviceFactorDataVO.class);

        return vos;
    }

    /**
     * @desc 查询因子的统计数据 - 传感器数据
     * @author fwx 2024/6/14 17:05
     **/
    public static PropertyDataVO countPropertyData(String deviceId, String factorName){
        UrlBuilder builder = UrlBuilder.of();
        builder.addQuery("deviceId",deviceId);
        builder.addQuery("factorName",factorName);
        String url = String.format("%s/device-property/properties/countData?%s",BASE_URL, builder.getQuery().toString());
        return getRremoteObject(PropertyDataVO.class,url);
    }

    /**
     * @desc 查询降雨的统计数据 - 传感器数据
     * @author fwx 2024/6/15 16:15
     **/
    public static List<DeviceFactorCountData> countRainData(DeviceRainCountRequest request){
        UrlBuilder builder = request.getQuery();
        String url = String.format("%s/device-property/properties/countData?%s",BASE_URL, builder.getQuery().toString());
        return getRremoteList(DeviceFactorCountData.class,url);
    }



    private static <T> T getRremoteObject(Class<T> clazz,String url){

        try {
            String body = HttpRequest.get(url)
                    .execute()
                    .body();

            String result = JSONObject.parseObject(body).getString("data");

            T vo = JSONObject.parseObject(result,clazz);
            return vo;
        } catch (HttpException e) {
            e.printStackTrace();
            throw new RuntimeException("远程调用失败");
        }
    }

    private static <T> List<T> getRremoteList(Class<T> clazz,String url){

        try {
            String body = HttpRequest.get(url)
                    .execute()
                    .body();

            String result = JSONObject.parseObject(body).getString("data");

            List<T> vos = JSONArray.parseArray(result, clazz);
            return vos;
        } catch (HttpException e) {
            e.printStackTrace();
            throw new RuntimeException("远程调用失败");
        }
    }


    public static void main(String[] args) {

        queryPhotoPage(null);

    }

}
